from .view import Dashboard, DashboardView, PanelibView, Panelib
from . import schema
from typing import List
from fastapi import APIRouter, Depends, Security
from package.connector.sql_db import db
from package.fastapi import permission


router_chart = APIRouter(prefix='/chart', tags=['图表管理'])
router_meta = {'module': '可视化', 'sub_module': '图表管理'}


@router_chart.get('/dashboard', response_model=schema.PageDashboards, openapi_extra=router_meta, name='dashboard_list_get',
                 summary='仪表板列表')
def dashboard_list_get(q: str = None,
             page: int = 1,
             page_size: int = 10,
             order_by: str = 'created_at desc',
             session=Depends(db.get_session),
             user=Security(permission.security, scopes=['dashboard:read'])):
    return DashboardView.get_page_items(session, q=q, page=page, page_size=page_size, order_by=order_by)


@router_chart.post('/dashboard', response_model=schema.DashboardOut, openapi_extra=router_meta, name='dashboard_post', summary='仪表板创建')
def dashboard_post(body: schema.CreateDashboard,
              session=Depends(db.get_session),
              user=Security(permission.security, scopes=['dashboard:write'])):
    return DashboardView.create_item(session, body=body.dict())


@router_chart.get('/dashboard/{id}', response_model=schema.DashboardOut, openapi_extra=router_meta, name='dashboard_get',
                 summary='仪表板详情')
def dashboard_get(id: str,
             session=Depends(db.get_session),
             user=Security(permission.security, scopes=['dashboard:read'])):
    return DashboardView.get_item(session, filter_=[Dashboard.id == id])


@router_chart.patch('/dashboard/{id}', response_model=schema.DashboardOut, openapi_extra=router_meta, name='dashboard_patch',
                   summary='仪表板修改')
def dashboard_patch(id: str,
               body: schema.EditDashboard,
               session=Depends(db.get_session),
               user=Security(permission.security, scopes=['dashboard:write'])):
    return DashboardView.update_item(session, filter_=[Dashboard.id == id], update=body.dict(exclude_unset=True))


@router_chart.delete('/dashboard', openapi_extra=router_meta, name='dashboard_delete', summary='仪表板删除')
def dashboard_delete(body: schema.BatchId,
                session=Depends(db.get_session),
                user=Security(permission.security, scopes=['dashboard:write'])):
    return {'count': DashboardView.delete_items(session, filter_=[Dashboard.id.in_(body.ids)])}


@router_chart.get('/dashboard/{id}/panel', response_model=List[schema.PanelibOut], openapi_extra=router_meta, name='get_dashboard_panel',
                  summary='获取仪表板下图表详情')
def get_dashboard_panel(id: str,
                          session=Depends(db.get_session),
                          user=Security(permission.security, scopes=['dashboard:read'])):
    return DashboardView.get_dashboard_panel(session, filter_=[DashboardView.id == id])


@router_chart.post('/dashboard/{id}/panel', response_model=schema.PanelibOut, openapi_extra=router_meta, name='post_dashboard_panel',
                  summary='仪表板下新增图表')
def post_dashboard_panel(body: schema.CreatePanelib,
                         session=Depends(db.get_session),
                         user=Security(permission.security, scopes=['dashboard:write'])):
    return PanelibView.create_item(session, body={**body.dict(), 'is_panelib': True})


@router_chart.delete('/dashboard/{id}/panel', openapi_extra=router_meta, name='delete_dashboard_panel',
                  summary='仪表板下图表移除')
def delete_dashboard_panel(body: schema.BatchId,
                           session=Depends(db.get_session),
                           user=Security(permission.security, scopes=['dashboard:write'])):
    return {'count': PanelibView.delete_items(session, filter_=[Panelib.id.in_(body.ids), PanelibView.is_panelib == False])}


# 图表
@router_chart.post('/search', openapi_extra=router_meta, name='panelib_data_search',
                 summary='库面板数据更新')
def panelib_data_search(body: schema.PanelibSearch):
    return PanelibView.get_item_search(**body.dict())


@router_chart.get('/panelib', response_model=schema.PagePanelibs, openapi_extra=router_meta, name='panelib_list_get',
                 summary='库面板列表')
def panelib_list_get(q: str = None,
             page: int = 1,
             page_size: int = 10,
             order_by: str = 'created_at desc',
             session=Depends(db.get_session),
             user=Security(permission.security, scopes=['panelib:read'])):
    return PanelibView.get_page_items(session, q=q, filter_=[PanelibView.is_panelib == True], page=page, page_size=page_size, order_by=order_by)


@router_chart.post('/panelib', response_model=schema.PanelibOut, openapi_extra=router_meta, name='panelib_post', summary='库面板创建')
def panelib_post(body: schema.CreatePanelib,
              session=Depends(db.get_session),
              user=Security(permission.security, scopes=['panelib:write'])):
    return PanelibView.create_panelib(session, body=body.dict())


@router_chart.get('/panelib/{id}', response_model=schema.PanelibOut, openapi_extra=router_meta, name='panelib_get',
                 summary='库面板详情')
def panelib_get(id: str,
             session=Depends(db.get_session),
             user=Security(permission.security, scopes=['panelib:read'])):
    return PanelibView.get_item(session, filter_=[Panelib.id == id, PanelibView.is_panelib == True])


@router_chart.patch('/panelib/{id}', response_model=schema.PanelibOut, openapi_extra=router_meta, name='panelib_patch',
                   summary='库面板修改')
def panelib_patch(id: str,
               body: schema.EditPanelib,
               session=Depends(db.get_session),
               user=Security(permission.security, scopes=['panelib:write'])):
    return PanelibView.update_panelib(session, filter_=[Panelib.id == id, PanelibView.is_panelib == True], update=body.dict(exclude_unset=True))


@router_chart.delete('/panelib', openapi_extra=router_meta, name='panelib_delete', summary='库面板删除')
def panelib_delete(body: schema.BatchId,
                session=Depends(db.get_session),
                user=Security(permission.security, scopes=['panelib:write'])):
    return {'count': PanelibView.delete_panelibs(session, filter_=[Panelib.id.in_(body.ids), PanelibView.is_panelib == True])}


@router_chart.get('/panelib/{id}/cache', response_model=schema.PanelibCacheOut, openapi_extra=router_meta, name='panelib_data_get',
                 summary='库面板数据获取')
def panelib_data_get(id: str,
                     session=Depends(db.get_session),
                     user=Security(permission.security, scopes=['panelib:read'])):
    return PanelibView.get_item_data(session, id)


@router_chart.put('/panelib/{id}/cache', response_model=schema.PanelibCacheOut, openapi_extra=router_meta, name='panelib_data_put',
                 summary='库面板数据更新')
def panelib_data_put(id: str, 
                     session=Depends(db.get_session),
                     body: schema.EditPanelibCache = None):
    return PanelibView.put_item_data(session, id, body.dict())
